package br.edu.ufcg.lp2;

import static org.junit.Assert.*;

import java.util.TreeSet;

import org.junit.Before;
import org.junit.Test;

public class GameTest {

	Game g1;
	User u, u2;
	
	@Before
	public void setUp() throws Exception {
		g1 = new Game("001");
		u = new User("user");
		u2 = new User("user2");
	}

	@Test
	public void testGetId() {
		assertEquals("001", g1.getId());
	}

	@Test
	public void testAddUser() {
		g1.addUser(u);
		assertEquals(u, g1.getUsers().get(0));
	}

	@Test
	public void testRemoveUser() {
		g1.addUser(u);
		g1.removeUser(u);
		assertEquals(0, g1.getUsers().size());
	}

	@Test
	public void testGetNamesByRating() throws Exception {
		g1.addUser(u);
		g1.addUser(u2);
		g1.like(new Name("Joao", "Corta pao"), u);
		g1.halflike(new Name("Pedro", "Corta pao"), u2);
		TreeSet<Name> teste = new TreeSet<Name>(new RatingComparator());
		Name n1 = new Name("Joao", "Corta pao");
		Name n2 = new Name("Pedro", "Corta pao");
		n1.addVote(new Vote(Vote.LIKE, u));
		n2.addVote(new Vote(Vote.HALFLIKE, u2));
		teste.add(n1);
		teste.add(n2);
		assertEquals(teste, g1.getNamesByRating());
	}

	@Test
	public void testLike() throws Exception {
		g1.addUser(u);
		g1.like(new Name("Joao", "Corta pao"), u);
		try{
			g1.like(new Name("Joao", "Corta pao"), u);
			fail("fail");
		} catch(Exception e){
			assertEquals("User has already voted for this name", e.getMessage());
		}
	}

	@Test
	public void testDislike() throws Exception {
		g1.addUser(u);
		g1.dislike(new Name("Joao", "Corta pao"), u);
		try{
			g1.dislike(new Name("Joao", "Corta pao"), u);
			fail("fail");
		} catch(Exception e){
			assertEquals("User has already voted for this name", e.getMessage());
		}
	}

	@Test
	public void testHalflike() throws Exception {
		g1.addUser(u);
		g1.halflike(new Name("Joao", "Corta pao"), u);
		try{
			g1.halflike(new Name("Joao", "Corta pao"), u);
			fail("fail");
		} catch(Exception e){
			assertEquals("User has already voted for this name", e.getMessage());
		}
	}

}
